//
//  PYPReachability.swift
//  DKDoctor
//
//  Created by Erickson on 16/2/19.
//  Copyright © 2016年 paiyipai. All rights reserved.
//

import UIKit



enum PYPReachabilityStatus {
    case wiFi
    case wwan
    case noReachable
}

class PYPReachability: NSObject {
    
    var reachability: Reachability?

    
    var status:PYPReachabilityStatus?
    
    override init(){
        
        super.init()
        
        
        NotificationCenter.default.addObserver(self,
            selector: #selector(PYPReachability.reachabilityChanged(_:)),
            name: Notification.Name.reachabilityChanged,
            object: reachability)
        
//        try! reachability!.startNotifier()

    }
    
    
    func reachabilityChanged(_ note: Notification) {
        
        let reachability = note.object as! Reachability
        
        if reachability.isReachable {
            if reachability.isReachableViaWiFi {
                self.status = .wiFi
                
                debugPrint("Reachable via WiFi")

            } else {
                self.status = .wwan
                debugPrint("Reachable via WWAN")
            }
        } else {
            self.status = .noReachable
            debugPrint("Not reachable")
        }
    }
  
    
    
    
    deinit {
        reachability!.stopNotifier()
        NotificationCenter.default.removeObserver(self,
                                                  name: Notification.Name.reachabilityChanged,
            object: reachability)
    }
}
